package com.changgou.service.goods.controller;

import com.changgou.common.pojo.PageResult;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
@CrossOrigin
public class BrandController {

    @Autowired
    private BrandService brandService;


    @GetMapping
    public Result<List<Brand>> findList(){
        List<Brand> brandList = brandService.findList();
        return new Result<List<Brand>>(true, StatusCode.OK,"查询成功",brandList);
    }


    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable("id") Integer id){

        Brand brand = brandService.findById(id);

        return new Result<Brand>(true, StatusCode.OK,"查询成功",brand);

    }

    @PostMapping
    public Result add(@RequestBody Brand brand){

        brandService.add(brand);
        return new Result(true, StatusCode.OK,"添加成功");

    }

    @PutMapping("/{id}")
    public Result edit(@PathVariable("id") Integer id ,@RequestBody Brand brand){
        brand.setId(id);
        brandService.edit(brand);
        return new Result(true, StatusCode.OK,"修改成功");

    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){

        brandService.deleteById(id);
        return new Result(true, StatusCode.OK,"删除成功");

    }

    @GetMapping("/search")
    public Result<List<Brand>> findByCondition(@RequestParam  Map searchMap){

        List<Brand> brandList = brandService.findByCondition(searchMap);

        return new Result<List<Brand>>(true, StatusCode.OK,"查询成功",brandList);
    }

    @GetMapping("/search/{page}/{size}")
    public Result<List<Brand>> findPage(@PathVariable  int page, @PathVariable int size ){

        Page<Brand> brandPage = brandService.findPage(page, size);
        PageResult<Brand> pageResult = new PageResult(brandPage.getTotal(),brandPage.getResult());
        return new Result<List<Brand>>(true, StatusCode.OK,"查询成功",pageResult);
    }

    @GetMapping("/searchPage/{page}/{size}")
    public Result<List<Brand>> findPageByCondition(@PathVariable  int page, @PathVariable int size ,@RequestParam Map searchMap){

        Page<Brand> brandPage = brandService.findPageByCondition(page, size,searchMap);
        PageResult<Brand> pageResult = new PageResult(brandPage.getTotal(),brandPage.getResult());
        return new Result<List<Brand>>(true, StatusCode.OK,"查询成功",pageResult);
    }

}
